//获取一个模拟的用户信息
function getAnalogUser(){
    return fetch('user/analogUser')
        .then(response => {
            return response.json();
        })
}
//请求当前用户信息, 失败: 跳转登录页面; 成功: 并返回user对象
function welcome(){
    return fetch('user/current-user')
        .then(response => {
            if (!response.ok) {
                throw new Error('Not logged in');
            }
            return response.json();
        })
        .then(user => {
            if (!user) { // 获取不到用户信息
                window.location.href = 'login.html';
                return user;
            }
            //成功获取用户信息
            return user;
        })
        .catch(error => {
            window.location.href = 'login.html';
            console.error('Error:', error);
            return null;
        });
}
//将小数转为百分比格式
function toPercentage(floatNumber, decimalPlaces = 1) {
    return (floatNumber * 100).toFixed(decimalPlaces) + '%';
}
// 初始化辖区选择框, 设置默认值为用户最大权限
function initDefaultPopedom(user){
    //搞不懂这个控件怎么用, 算了
    const selectProvince  = document.getElementById('province');
    selectProvince.value = "3";
    const selectCity = document.getElementById('city');
    const selectDistrict = document.getElementById('district');
    const selectBlock = document.getElementById('block');
}
//为摄像头列表赋初值
function initCameraList(user) {
    //为可选列表赋值
    $.ajax({
        url: "camera/getCameras",
        type: "post",
        data: {
            provinceId: user.provinceId ,
            cityId: user.cityId ,
            districtId: user.districtId ,
            blockId: user.blockId
        },
        success: function (cameras){
            $('#camera').combobox('loadData', JSON.parse(cameras));//加载数据对象而不是json字符串
        }
    });
    //为已选列表赋值
    $.ajax({
        url: "camera/selected",
        type: "post",
        dataType: 'json',
        success: function (data) {
            // 将数据赋值给 easyui-combobox
            $('#camera').combobox('setValues', data);
        },
        error: function (error) {
            console.error('请求出错:', error);
        },
        data: {
            userId:  user.id,
        }
    })
}
//选好block后会被调用，为摄像头列表赋值
function getCameras(){
    //console.log("选好block后，为摄像头列表赋值")
    const provinceId = $('#province').combobox('getValue');// $('#province option:selected').val()
    const cityId = $('#city').combobox('getValue');
    const districtId = $('#district').combobox('getValue');
    const blockId = $('#block').combobox('getValue');
    //console.log("参数列表：")
    //console.log(provinceId, cityId, districtId, blockId)
    $.ajax({
        url: "camera/getCameras",
        type: "post",
        data: {
            provinceId : provinceId,
            cityId : cityId,
            districtId: districtId,
            blockId: blockId
        },
        success: function (cameras){
            $('#camera').combobox('loadData', JSON.parse(cameras));//加载数据对象而不是json字符串
        }
    })
}
//退出系统
function exitSystem(){
    $.ajax({
        url: "user/logout",
        type: "POST",
        success: function(response) {
            response=JSON.parse(response);
            if (response.success) {
                // 注销成功，退出系统
                window.location.href = "login.html";
            } else {
                alert("注销失败：" + response.message);
            }
        },
        error: function(xhr, status, error) {
            alert("请求失败：" + status + " " + error);
        }
    });

}
